package com.zjdiepu.www.proxy.biz.impl.transfer;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.config.ScheduleProxyConfig;
import com.zjdiepu.www.enums.TimedTaskStatus;
import com.zjdiepu.www.enums.TransferModeID;
import com.zjdiepu.www.proxy.biz.transfer.INewTransaction;
import com.zjdiepu.www.proxy.dao.transfer.TransferOtherTimedTaskMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferOtherTimedTask;
/**
 * 
 * @author xiongxiaotun
 *
 */
@Service
@Transactional(isolation=Isolation.READ_COMMITTED,readOnly=false,propagation=Propagation.REQUIRES_NEW,timeout=60)
public class NewTransactionImpl implements INewTransaction {
	@Resource
	private ScheduleProxyConfig scheduleProxyConfig;
	
	@Resource
	private TransferOtherTimedTaskMapper transferOtherTimedTaskMapper;
	/**
	 * 同步推送状态到主程序
	 */
	@Override
	public void savePushStatus(String orderno, String busiId) {
		TransferOtherTimedTask diviStatusTask = new TransferOtherTimedTask();
		diviStatusTask.setModeID(TransferModeID.GENG_XIN_MIAN_QIAN_FU_JIAN_TUI_SONG_ZHUANG_TAI.getCode());
		diviStatusTask.setDataid(busiId);
		diviStatusTask.setBankOrderNo(orderno);
		diviStatusTask.setTiimeExecute(DateUtil.increaseHour(new Date(), scheduleProxyConfig.getSynPushStatus()));
		diviStatusTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		diviStatusTask.initInsertData(SystemConsts.DEFAULT_ID, "同步推送状态到主程序");
		transferOtherTimedTaskMapper.insert(diviStatusTask);
	}
	
	/**
	 * 容联云下载面签视频
	 */
	@Override
	public void saveDownloadInterviewMedia(String orderno, String busiId) {
		TransferOtherTimedTask diviStatusTask = new TransferOtherTimedTask();
		diviStatusTask.setModeID(TransferModeID.M_TRANSFER_XZMQFJ.getCode());
		diviStatusTask.setDataid(busiId);
		diviStatusTask.setBankOrderNo(orderno);
		diviStatusTask.setTiimeExecute(DateUtil.increaseHour(new Date(), scheduleProxyConfig.getDownlaodInterviewMedia()));
		diviStatusTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		diviStatusTask.initInsertData(SystemConsts.DEFAULT_ID, "容联云下载面签视频");
		transferOtherTimedTaskMapper.insert(diviStatusTask);
	}
	
	/**
	 * 查询申请分期成功后推送面签多媒体到工行
	 */
	@Override
	public void saveSendMultiMediaUpload(String orderno, String busiId) {
		TransferOtherTimedTask diviStatusTask = new TransferOtherTimedTask();
		diviStatusTask.setModeID(TransferModeID.TUI_SONG_MIAN_QIAN_SHI_PIN.getCode());
		diviStatusTask.setDataid(busiId);
		diviStatusTask.setBankOrderNo(orderno);
		diviStatusTask.setTiimeExecute(DateUtil.increaseHour(new Date(), scheduleProxyConfig.getSendInterviewInfo()));
		diviStatusTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		diviStatusTask.initInsertData(SystemConsts.DEFAULT_ID, "向工行推送面签视频");
		transferOtherTimedTaskMapper.insert(diviStatusTask);
	}
	/**
	 * 查询申请分期失败后处理
	 */
	@Override
	public void saveAfterApplyDiviStatusHzicbc(String orderno, String busiId) {
		TransferOtherTimedTask diviStatusTask = new TransferOtherTimedTask();
		diviStatusTask.setModeID(TransferModeID.CXFQZT.getCode());
		diviStatusTask.setDataid(busiId);
		diviStatusTask.setBankOrderNo(orderno);
		diviStatusTask.setTiimeExecute(DateUtil.increaseDate(new Date(), scheduleProxyConfig.getApplyDiviStatusHzicbc() / 1000 / 60 / 60 / 24));
		diviStatusTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		diviStatusTask.initInsertData(SystemConsts.DEFAULT_ID, "查询分期状态接口");
		transferOtherTimedTaskMapper.insert(diviStatusTask);
	}
	
	/**
	 * 同步附件列表
	 */
	@Override
	public void saveSynAttachementsDiepu(String orderno, String busiId) {
		TransferOtherTimedTask hqfjlbTask = new TransferOtherTimedTask();
		hqfjlbTask.setModeID(TransferModeID.HQFJLB.getCode());
		hqfjlbTask.setDataid(busiId);
		hqfjlbTask.setBankOrderNo(orderno);
		hqfjlbTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), scheduleProxyConfig.getSynAttachementsDiepu() / 1000 / 60));
		hqfjlbTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		hqfjlbTask.initInsertData(SystemConsts.DEFAULT_ID, "申请分期接口");
		transferOtherTimedTaskMapper.insert(hqfjlbTask);
	}
	
	/**
	 * 同步七牛文件
	 */
	@Override
	public void saveFileDownloadQiniu(String orderno, String busiId) {
		TransferOtherTimedTask xzfjTask = new TransferOtherTimedTask();
		xzfjTask.setModeID(TransferModeID.XZFJ.getCode());
		xzfjTask.setDataid(busiId);
		xzfjTask.setBankOrderNo(orderno);
		xzfjTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), scheduleProxyConfig.getFileDownloadQiniu() / 1000 / 60));
		xzfjTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		xzfjTask.initInsertData(SystemConsts.DEFAULT_ID, "申请分期接口");
		transferOtherTimedTaskMapper.insert(xzfjTask);
	}
	
	/**
	 * 申请工行分期:首次
	 */
	@Override
	public void saveApplyDiviHzicbcFirst(String orderno, String busiId) {
		TransferOtherTimedTask sqfqTask = new TransferOtherTimedTask();
		sqfqTask.setModeID(TransferModeID.SQFQ.getCode());
		sqfqTask.setDataid(busiId);
		sqfqTask.setBankOrderNo(orderno);
		sqfqTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), scheduleProxyConfig.getApplyDiviHzicbcFirst() / 1000 / 60));
		sqfqTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		sqfqTask.initInsertData(SystemConsts.DEFAULT_ID, "申请分期接口");
		transferOtherTimedTaskMapper.insert(sqfqTask);
	}
	
	/**
	 * 申请工行分期：重试
	 */
	@Override
	public void saveApplyDiviHzicbcRetry(String orderno, String busiId) {
		TransferOtherTimedTask sqfqTask = new TransferOtherTimedTask();
		sqfqTask.setModeID(TransferModeID.SQFQ.getCode());
		sqfqTask.setDataid(busiId);
		sqfqTask.setBankOrderNo(orderno);
		sqfqTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), scheduleProxyConfig.getApplyDiviHzicbcRetry() / 1000 / 60));
		sqfqTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		sqfqTask.initInsertData(SystemConsts.DEFAULT_ID, "申请分期接口");
		transferOtherTimedTaskMapper.insert(sqfqTask);
	}
	
	/**
	 * 插入一个 10分钟之后的【向主程序通知开卡成功】的定时任务
	 * @param orderno
	 * @param busiId
	 */
	@Override
	public void saveOpenCardSuccess(String orderno,String busiId){
		TransferOtherTimedTask sqfqTask = new TransferOtherTimedTask();
		sqfqTask.setModeID(TransferModeID.TZKKCG.getCode());
		sqfqTask.setDataid(busiId);
		sqfqTask.setBankOrderNo(orderno);
		sqfqTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), scheduleProxyConfig.getOpenCardSussess() / 1000 / 60));
		sqfqTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		sqfqTask.initInsertData(SystemConsts.DEFAULT_ID, "通知开卡成功");
		transferOtherTimedTaskMapper.insert(sqfqTask);
	}
	/**
	 * 插入一个 10分钟之后的【向主程序通知开卡失败】的定时任务
	 * @param orderno
	 * @param busiId
	 */
	@Override
	public void saveOpenCardFailTask(String orderno,String busiId){
		TransferOtherTimedTask sqfqTask = new TransferOtherTimedTask();
		sqfqTask.setModeID(TransferModeID.TZKKSB.getCode());
		sqfqTask.setDataid(busiId);
		sqfqTask.setBankOrderNo(orderno);
		sqfqTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), scheduleProxyConfig.getOpenCardSussess() / 1000 / 60));
		sqfqTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		sqfqTask.initInsertData(SystemConsts.DEFAULT_ID, "通知开卡失败");
		transferOtherTimedTaskMapper.insert(sqfqTask);
	}
	
	/**
	 * 2分钟任务
	 * @param orderno
	 * @param busiId
	 */
	@Override
	public void saveHqfjlbTaskWith2Minutes(String bankOrderNo,String busiId,int minute) {
		TransferOtherTimedTask transferOtherTimedTask = new TransferOtherTimedTask();
		transferOtherTimedTask.setModeID(TransferModeID.HQFJLB.getCode());
		transferOtherTimedTask.setDataid(busiId);
		transferOtherTimedTask.setBankOrderNo(bankOrderNo);
		transferOtherTimedTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), minute));
		transferOtherTimedTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		transferOtherTimedTask.initInsertData(SystemConsts.DEFAULT_ID, "获取附件列表");
		transferOtherTimedTaskMapper.insert(transferOtherTimedTask);
	}
	
	/**
	 * 5分钟任务
	 * @param orderno
	 * @param busiId
	 */
	@Override
	public void xzfjTaskWith5Minutes(String bankOrderNo,String busiId,int minute) {
		TransferOtherTimedTask transferOtherTimedTask = new TransferOtherTimedTask();
		transferOtherTimedTask.setModeID(TransferModeID.XZFJ.getCode());
		transferOtherTimedTask.setDataid(busiId);
		transferOtherTimedTask.setBankOrderNo(bankOrderNo);
		transferOtherTimedTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), minute));
		transferOtherTimedTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		transferOtherTimedTask.initInsertData(SystemConsts.DEFAULT_ID, "下载附件");
		transferOtherTimedTaskMapper.insert(transferOtherTimedTask);
	}
	
	/**
	 * 10分钟任务
	 * @param orderno
	 * @param busiId
	 */
	@Override
	public void sqczxTaskWith10Minute(String bankOrderNo,String busiId,int minute) {
		TransferOtherTimedTask transferOtherTimedTask = new TransferOtherTimedTask();
		transferOtherTimedTask.setModeID(TransferModeID.SQCZX.getCode());
		transferOtherTimedTask.setDataid(busiId);
		transferOtherTimedTask.setBankOrderNo(bankOrderNo);
		transferOtherTimedTask.setTiimeExecute(DateUtil.increaseMinute(new Date(), minute));
		transferOtherTimedTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		transferOtherTimedTask.initInsertData(SystemConsts.DEFAULT_ID, "申请查征信");
		transferOtherTimedTaskMapper.insert(transferOtherTimedTask);
	}
	
	/**
	 * 24小时任务
	 * @param bankOrderNo
	 * @param busiId
	 * @param minute
	 */
	@Override
	public void cxfxzbjyeTaskWith24Hours(String bankOrderNo,String busiId,int minute) {
		TransferOtherTimedTask transferOtherTimedTask = new TransferOtherTimedTask();
		transferOtherTimedTask.setModeID(TransferModeID.CXFXZBJYE.getCode());
		transferOtherTimedTask.setDataid(busiId);
		transferOtherTimedTask.setBankOrderNo(bankOrderNo);
//		update 2018-03-07 分钟改成小时
		transferOtherTimedTask.setTiimeExecute(DateUtil.increaseHour(new Date(), minute));
		transferOtherTimedTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		transferOtherTimedTask.initInsertData(SystemConsts.DEFAULT_ID, "查询风险准备金余额");
		transferOtherTimedTaskMapper.insert(transferOtherTimedTask);
	}
	
	/**
	 * 向工行申请开卡10分钟
	 * @param orderno
	 * @param busiId
	 */
	@Override
	public void saveToHzicbc(String orderno, String busiId,Date date) {
		TransferOtherTimedTask diviStatusTask = new TransferOtherTimedTask();
		diviStatusTask.setModeID(TransferModeID.SQKK.getCode());
		diviStatusTask.setDataid(busiId);
		diviStatusTask.setBankOrderNo(orderno);
		diviStatusTask.setTiimeExecute(date);
		diviStatusTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		diviStatusTask.initInsertData(SystemConsts.DEFAULT_ID, "向工行申请开卡");
		transferOtherTimedTaskMapper.insert(diviStatusTask);
	}
	
	/**
	 * 获取附件列表2分钟
	 * @param bankOrderNo
	 * @param busiId
	 * @param date
	 */
	@Override
	public void getAttachments(String bankOrderNo,String busiId,Date date) {
		TransferOtherTimedTask transferOtherTimedTask = new TransferOtherTimedTask();
		transferOtherTimedTask.setModeID(TransferModeID.HQFJLB.getCode());
		transferOtherTimedTask.setDataid(busiId);
		transferOtherTimedTask.setBankOrderNo(bankOrderNo);
		transferOtherTimedTask.setTiimeExecute(date);
		transferOtherTimedTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		transferOtherTimedTask.initInsertData(SystemConsts.DEFAULT_ID, "获取附件列表");
		transferOtherTimedTaskMapper.insert(transferOtherTimedTask);
	}
	
	/**
	 * 下载附件5分钟
	 * @param bankOrderNo
	 * @param busiId
	 * @param date
	 */
	@Override
	public void downLoadAttachments(String bankOrderNo,String busiId,Date date) {
		TransferOtherTimedTask transferOtherTimedTask = new TransferOtherTimedTask();
		transferOtherTimedTask.setModeID(TransferModeID.XZFJ.getCode());
		transferOtherTimedTask.setDataid(busiId);
		transferOtherTimedTask.setBankOrderNo(bankOrderNo);
		transferOtherTimedTask.setTiimeExecute(date);
		transferOtherTimedTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		transferOtherTimedTask.initInsertData(SystemConsts.DEFAULT_ID, "下载附件");
		transferOtherTimedTaskMapper.insert(transferOtherTimedTask);
	}
	
	/**
	 * 查询开卡进度
	 * @param bankOrderNo
	 * @param busiId
	 * @param date
	 */
	@Override
	public void queryOpenCardCourse(String bankOrderNo,String busiId,Date date) {
		TransferOtherTimedTask transferOtherTimedTask = new TransferOtherTimedTask();
		transferOtherTimedTask.setModeID(TransferModeID.CXKKJD.getCode());
		transferOtherTimedTask.setDataid(busiId);
		transferOtherTimedTask.setBankOrderNo(bankOrderNo);
		transferOtherTimedTask.setTiimeExecute(date);
		transferOtherTimedTask.setTimedTaskStatus(TimedTaskStatus.WEI_ZHI_XING.getCode());
		transferOtherTimedTask.initInsertData(SystemConsts.DEFAULT_ID, "下载附件");
		transferOtherTimedTaskMapper.insert(transferOtherTimedTask);
	}
}
